package org.flashbrighton.as3bots.bots
{	
	import avmplus.getQualifiedClassName;
	
	import flash.display.Sprite;
	import flash.globalization.LastOperationStatus;
	import flash.system.ApplicationDomain;
	
	import org.flashbrighton.as3bots.components.*;
	import org.flashbrighton.as3bots.events.BombSensorEvent;
	import org.flashbrighton.as3bots.events.CollectGemEvent;
	import org.flashbrighton.as3bots.events.GemSensorEvent;
	import org.flashbrighton.as3bots.events.HitBombEvent;
	import org.flashbrighton.as3bots.events.HitBotEvent;
	import org.flashbrighton.as3bots.events.HitWallEvent;
	import org.flashbrighton.as3bots.events.ScanBotEvent;
	import org.flashbrighton.as3bots.models.BotData;
	import org.flashbrighton.as3bots.views.ui.Message;
	
	
	public class BaseBot implements IBot
	{
		public static const SMALL_ANGLE_MAX_BY_STEP:Number =  0.06404499509244;
		public static const MEDIUM_ANGLE_MAX_BY_STEP:Number = 0.03088589655306;
		public static const LARGE_ANGLE_MAX_BY_STEP:Number =  0.01667143770627;
		
		public var size:String;
		public var colour:Number;
		public var skin:Sprite;
		
		private var _controller:IBot;		
		private var _components:Array;

		public var steeringPower:Number=0;		
		public var enginePower:Number=0;
		
		private var _nbBombs:int = 5;
		private var _dropBombRequest:Boolean = false;
				
		public function BaseBot(additionalComponent:String=null, color:Number=0xFFFFFF, size:String=ChassisSize.MEDIUM)
		{
			this.colour = color;
			this.size = size;
			
			_components = new Array();
			addEssentialComponents();
			addComponent(additionalComponent);
		}		
		
		final public function get nbBombs():int{
			return _nbBombs;
		}

		final public function get botName():String{
			var name:String = getQualifiedClassName(this);
			var start:int = Math.max(name.lastIndexOf(":")+1, name.lastIndexOf(".")+1);
			return name.substring(start, name.length -3);
		}
		
		public function getEnginePower():Number
		{
			return enginePower;
		}

		public function getSteeringPower():Number
		{
			return steeringPower;
		}
		
		public function onHitBot(event:HitBotEvent):void
		{
		}
		
		public function onHitWall(event:HitWallEvent):void
		{
		}

		public function onHitBomb(event:HitBombEvent):void
		{
		}

		public function onScanBot(event:ScanBotEvent):void
		{
		}
		
		public function onCollectGem(event:CollectGemEvent):void
		{
		}

		public function onGemSensor(event:GemSensorEvent):void
		{
		}

		public function onBombSensor(event:BombSensorEvent):void
		{
		}

		public function update(data:BotData):void
		{
		}
		
		private function addComponent(name:String):void
		{
			if (_components.length == ComponentManager.MAX_NUM) {
				throw new Error("Too may components added !");
			}
			if (_components.indexOf(name) > -1) return;
			
			_components.push(name);
		}
		
		
		final public function get components():Array
		{
			return _components;
		}
		
		
		final public function get controller():IBot
		{
			return this;
		}
		
		
		private function addEssentialComponents():void
		{
			addComponent(CoreComponents.ENGINE);
			addComponent(CoreComponents.STEERING);
			addComponent(CoreComponents.HEALTH);
			addComponent(CoreComponents.CHASSIS);
		}
		
		/**
		 * Write a message to the wall during the fight
		 * 
		 */
		public function writeMessage(msg:String):void{
			if(Message.tf==null){
				trace(msg)
			}else{
				Message.write(this, msg);
			}
		}
		
		/**
		 * Drop a bomb
		 */
		final public function dropBomb():void{
			if(_nbBombs>0){
				_nbBombs--;
				_dropBombRequest = true;
			}
		}
		
		final public function get dropBombRequest():Boolean{
			if(_dropBombRequest){
				//reset dropBombRequest
				_dropBombRequest = false;
				return true;
			}else{
				return false;
			}
		}
	}
}